<?php
require_once("lib.php");

$action_id = isset($_REQUEST['action_id']) && is_numeric ($_REQUEST['action_id']) ? intval($_REQUEST['action_id']) : 0;
$item_idx = isset($_REQUEST['item_id']) && is_numeric ($_REQUEST['item_id']) ? intval($_REQUEST['item_id']) : 0;
$mini_cart = isset($_REQUEST['mini']) && is_numeric($_REQUEST['mini']) ? intval($_REQUEST['mini']) : 0;

$item_ix = $item_idx % 10;
$item_id = intval($item_idx / 10);

$cart = &get_cart();
$lst = &$cart['lst'];

if($action_id > 0) {
    if($action_id == 255) { //clear cart
        array_splice($lst, 0);
        $cart['ajs']['tip'] = 0.0;
        $cart['seq'] = 0;
        
    } else if($action_id == 200) { //adjust tip
        $tip = isset($_REQUEST['tip']) && is_numeric ($_REQUEST['tip']) ? floatval($_REQUEST['tip']) : 0;
        $cart['ajs']['tip'] = $tip > 0 ? round($tip, 2) : 0;
        $cart['seq'] = 0;
        
    }
    
    if($item_id >= 0 && $item_id < count($g_menu)) {
        if($item_ix < 0 || $item_ix >= count($g_menu[$item_id][1])) $item_ix = 0;
        $item_idx = $item_id * 10 + $item_ix;
        
        for($i = 0; $i < count($lst); $i++)
            if($lst[$i]['idx'] === $item_idx ) break;
        
        if($action_id == 1) {//set
            $qty = isset($_REQUEST['qty']) && is_numeric ($_REQUEST['qty']) ? intval($_REQUEST['qty']) : 1;
            if($qty < 0) $qty = 0;
            
            if($i >= count($lst)) {
                $lst[] = array('idx'=> $item_idx, 'qty'=> $qty);
            } else {
                $lst[$i]['qty'] = $qty;
            }
            
            $cart['seq'] = 0;
            
        } else if($action_id == 2) {//add
            $qty = isset($_REQUEST['qty']) && is_numeric ($_REQUEST['qty']) ? intval($_REQUEST['qty']) : 1;
            if($qty < 0) $qty = 0;
            
            if($i >= count($lst)) {
                $lst[] = array('idx'=> $item_idx, 'qty'=> $qty);
            } else {
                $lst[$i]['qty'] += $qty;
            }
            
            $cart['seq'] = 0;
            
        } else if($action_id == 10) {//inc or dec
            if($i < count($lst)) {
                $_REQUEST['v'] > 0 ? $lst[$i]['qty']++ : $lst[$i]['qty']--;
                $cart['seq'] = 0;
            }
            
        } else if($action_id == 20) {//remove
            if($i < count($lst)) {
                array_splice($lst, $i, 1);
                $cart['seq'] = 0;
            }
            
        }
    
    }
}

validate_cart();
$cart_total = get_cart_total();

if($mini_cart) {
    $rjs = array(
        'lst_len'=> count($lst),
        'sub_total' => $cart_total['sub_total'],
        'total' => $cart_total['total'],
        'oa_total' => $cart_total['oa_total'],
    );
} else {
    $rjs = array(
        'lst'=> $lst,
        'lst_len'=> count($lst),
        'sub_total' => $cart_total['sub_total'],
        'total' => $cart_total['total'],
        'oa_total' => $cart_total['oa_total'],
        'ajs' => $cart['ajs']
    );
}


$user_record = isset($_REQUEST['user_record']) && is_numeric($_REQUEST['user_record']) ? intval($_REQUEST['user_record']) : 0;
$cur_user = &get_user();

$user = array(
'id' => $cur_user['user_id'],
'acct' => $cur_user['user_acct'],
'name' => isset($cur_user['user_js']['name']) ? $cur_user['user_js']['name'] : '',
);

if($user_record && $cur_user['user_id']) {
    $js = $cur_user['user_js'];
    
    $card_info = $js['card_info'];
    $user['order_info'] = $js['order_info'];
    
    $ci = array(
        'name'=> $card_info['name'],
        'address'=> $card_info['address'],
    );
    
    $dt = getdate();
    if( $card_info['year'] * 10000 + $card_info['month'] < $dt['year'] * 10000 + $dt['month'] )
        $ci['number'] = '';
    else {
        $n = strlen($card_info['number']) - 4;
        $ci['number'] = str_repeat('*', $n < 0 ? 0 : $n) . substr($card_info['number'], -4);
    }
    
    $user['card_info'] = $ci;
}

$rjs['user'] = $user;
print json_encode($rjs);


?>